#! /bin/bash

# 此脚本用来打debug的ipa包，会自动传fir

ensure_git_status_clean() {
    if output=$(git status --porcelain) && [ -z "$output" ]; then
        echo "👍 Git is clean, start the next step."
    else
        echo "oh my God, An error occurred, please ensure git status clean"
        exit 0
    fi
}

build_ipa() {

    # 项目路径
    projectPath="./TLCLockDemo/TLCLockDemo.xcodeproj"

    # 开发者账号
    developerTeamID="X7S38PP265"

    # 配置名称，例如：Release
    configuration="Release"

    # 清理项目
    xcodebuild clean -project "$projectPath" -configuration "$configuration"

    # 构建项目
    xcodebuild archive -project "$projectPath" -scheme "TLCLockDemo" -configuration "$configuration" -archivePath "TLCLockDemo.xcarchive"

    # 导出 IPA
    xcodebuild -exportArchive -archivePath "TLCLockDemo.xcarchive" -exportOptionsPlist exportOptions.plist -exportPath "./"
}

echo "----检查环境----"

ensure_git_status_clean

echo "----👍环境OK----"

echo -e "\n\n开始打包"
echo -e "\n\n\n ------------------------------------------------------ \n\n\n"

git=`sh /etc/profile; which git`
branchName=`"$git" rev-parse --abbrev-ref HEAD`

echo -e "\n当前分支：$branchName"

build_ipa

fir publish ./TLCLockDemo.ipa --token="a0e2f901f860447c110b3a411559c7e5" --feishu-access-token="beadc7f7-879d-4e4a-bb5f-f9b3d164ce24" --feishu-custom-message="分支:$branchName"
